package com.moyuduo.java8.time;

import java.time.DayOfWeek;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetTime;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;

import org.junit.Test;

/**
*@author litao,School of computer and software engineering,Xihua University
*@date 2020年3月30日
*java8新增LocalDate,LocalTime,LocalDateTime来分别获取本地日期、本地时间、本地日期时间
*时间戳Instant
*计算时间间隔的Duration和计算日期间隔的Period
*TemporalAdjuster矫正日期
*DateTimeFormatter格式化日期时间
*ZoneDate、ZoneTime、ZoneDateTime来操作时区
*/
public class TimeTest {

	/**
	 * LocalDate
	 */
	@Test
	public void test1() {
		LocalDate date1 = LocalDate.now();
		System.out.println(date1);//2020-03-30
		
		LocalDate plusYears = date1.plusYears(1);
		System.out.println(plusYears);//2021-03-30
		
		LocalDate minusDays = date1.minusDays(2);
		System.out.println(minusDays);//2020-03-28
		
		LocalDate date2 = LocalDate.of(2019, 3, 30);
		System.out.println(date2.getYear());//2019
	}
	
	/**
	 * LocalTime
	 */
	@Test
	public void test2() {
		LocalTime now = LocalTime.now();
		System.out.println(now);//21:15:23.418
		
		int minute = now.getMinute();
		System.out.println(minute);//15
		
		int second = now.getSecond();
		System.out.println(second);//23
		
		LocalTime of = LocalTime.of(10, 10, 10);
		System.out.println(of);//10:10:10
		
		LocalTime minusMinutes = of.minusMinutes(2);
		System.out.println(minusMinutes);//10:08:10
		
		LocalTime plusHours = of.plusHours(2);
		System.out.println(plusHours);//12:10:10
	}
	
	/**
	 * LocalDateTime
	 */
	@Test
	public void test3() {
		LocalDateTime now = LocalDateTime.now();
		System.out.println(now);//2020-03-30T21:20:37.961
		
		int minute = now.getMinute();
		System.out.println(minute);//20
		
		LocalDateTime plusMinutes = now.plusMinutes(20);
		System.out.println(plusMinutes);//2020-03-30T21:40:37.961
		
		LocalDateTime minusYears = now.minusYears(2);
		System.out.println(minusYears);//2018-03-30T21:20:37.961
		
		LocalDateTime of = LocalDateTime.of(2021, 3, 30, 21, 19, 50);
		System.out.println(of);//2021-03-30T21:19:50
	}
	
	/**
	 * Instant
	 */
	@Test
	public void test4() {
		Instant now = Instant.now();
		System.out.println(now);//2020-03-30T13:26:10.640Z
		
		Instant plusSeconds = now.plusSeconds(10);
		System.out.println(plusSeconds);//2020-03-30T13:26:20.640Z
		
		//获取时间戳相对于1970年0时0分0秒的毫秒数
		long epochMilli = plusSeconds.toEpochMilli();
		System.out.println(epochMilli);//1585574780640
	}
	
	/**
	 * Duration
	 */
	@Test
	public void test5() {
		Instant start = Instant.now();
		Instant end = start.plusSeconds(10);
		Duration duration = Duration.between(start, end);
		long seconds = duration.getSeconds();
		//获取时间间隔的秒数
		System.out.println(seconds);//10
		
		//获取时间间隔的毫秒数
		long millis = duration.toMillis();
		System.out.println(millis);//10000
	}
	
	/**
	 * Period
	 */
	@Test
	public void test6() {
		LocalDate date1 = LocalDate.now();
		LocalDate date2 = date1.plusYears(2);
		Period period = Period.between(date1, date2);
		//获取两时间间隔的月数,指两个月份的间隔数，并不是时间间隔的总月数
		int months = period.getMonths();
		System.out.println(months);//0
	}
	
	/**
	 * TemporalAdjuster矫正日期
	 */
	@Test
	public void test7() {
		LocalDate date1 = LocalDate.now();
		System.out.println(date1);//2020-03-31
		
		//TemporalAdjusters类中封装了一些常用地时间矫正方法
		TemporalAdjuster next = TemporalAdjusters.next(DayOfWeek.FRIDAY);
		LocalDate date2 = date1.with(next);
		System.out.println(date2);//2020-04-03
		
		//自定义时间矫正器
		LocalDate date3 = date1.with(x->{
			LocalDate ld=(LocalDate)x;
			DayOfWeek dayOfWeek = ld.getDayOfWeek();
			if(dayOfWeek.equals(DayOfWeek.FRIDAY)) {
				return ld.plusDays(3);
			}else if(dayOfWeek.equals(DayOfWeek.SATURDAY)) {
				return ld.plusDays(2);
			}else {
				return ld.plusDays(1);
			}
		});
		System.out.println(date3);//2020-04-01
	}
	
	/**
	 * DateTimeFormatter对时间日期进行格式化
	 */
	@Test
	public void test8() {
		LocalDateTime dateTime1 = LocalDateTime.now();
		System.out.println(dateTime1);//2020-03-31T18:28:04.256
		
		DateTimeFormatter formatter1 = DateTimeFormatter.ISO_DATE;
		String format1 = dateTime1.format(formatter1);
		System.out.println(format1);//2020-03-31
		
		DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy年MM月dd日HH时mm分ss秒");
		String format2 = dateTime1.format(formatter2);
		System.out.println(format2);//2020年03月31日18时28分04秒
		
		LocalDateTime dateTime2 = LocalDateTime.parse(format2, formatter2);
		System.out.println(dateTime2);//2020-03-31T18:28:04
	}
	
	/**
	 * ZoneDate
	 */
	@Test
	public void test9() {
		//获取可用时区
		ZoneId.getAvailableZoneIds().forEach(System.out::println);
		
		LocalDate date1 = LocalDate.now(ZoneId.of("Asia/Tokyo"));
		System.out.println(date1);//2020-03-31
	}
	
	/**
	 * ZoneTime
	 */
	@Test
	public void test10() {
		LocalTime time1 = LocalTime.now(ZoneId.of("Asia/Tokyo"));
		System.out.println(time1);//19:44:15.228
		
		OffsetTime atOffset = time1.atOffset(ZoneOffset.ofHours(2));
		System.out.println(atOffset);//19:44:15.228+02:00
	}
	
	/**
	 * ZoneDateTime
	 */
	@Test
	public void test11() {
		LocalDateTime dateTime1 = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
		System.out.println(dateTime1);//2020-03-31T18:51:07.136
		
		ZonedDateTime atZone = dateTime1.atZone(ZoneId.of("Asia/Shanghai"));
		System.out.println(atZone);//2020-03-31T18:51:07.136+08:00[Asia/Shanghai]	+09:00表示时间比格林尼治时间快9小时
	}
}
